package
{
	import org.flixel.*;

	public class ChallengeListing extends FlxGroup
	{
		public static const WIDTH:int  = 300;
		private static const CHALLENGED:int = 1;
		private static const UNCHALLENGED:int = 0;
		
		private var nameText:FlxText;
		private var button:FlxSprite;
		private var checkmark:FlxSprite;
		private var status:int;
		
		private var startX:Number;
		private var startY:Number;
		private var name:String;
		private var index:int;
		
		public function ChallengeListing(name:String, index:int, status:int, x:Number, y:Number)
		{
			startX = x;
			startY = y;
			this.name = name;
			this.index = index;
			this.status = status;
			
			initLayout();
		}
		
		/**
		 * Add the name text and button (initially challenged).
		 */
		public function initLayout():void {
			button = new FlxSprite(startX, startY, Res.Checkbox);
			add(button);
			
			nameText = new FlxText(button.x + button.width + 10, button.y, WIDTH, name, false);
			nameText.setFormat("Georgia", 20, 0x000000, "left");
			add(nameText);
			
			checkmark = new FlxSprite(button.x, button.y - 5, Res.SmallCheckmark);
			
			if (status == CHALLENGED) {
				add(checkmark);
			}
		}
		
		/**
		 * Get the friend's challenge status
		 */
		public function getStatus():int {
			return status;
		}
		
		/**
		 * Get the friend's indexing in ChallengeFriendsComponent
		 */
		public function getIndex():int { 
			return index;
		}
		
		/**
		 * Get the friend's name (for debugging)
		 */
		public function getName():String {
			return name;
		}
		
		/**
		 * Toggle the button from being challenged/unchallenged
		 */
		public override function update():void {
			super.update();
			
			if (FlxG.mouse.justPressed() && ((button.overlapsPoint(FlxG.mouse.screenX, FlxG.mouse.screenY))
										|| (nameText.overlapsPoint(FlxG.mouse.screenX, FlxG.mouse.screenY)))) {
				if (status == CHALLENGED) {
					status = UNCHALLENGED;
					remove(checkmark);
				} else if (status == UNCHALLENGED) {
					status = CHALLENGED;
					add(checkmark);
				}
			}
		}
	}
}